//
//  MusicHistoryCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/25.
//

import UIKit

class MusicHistoryCell: UITableViewCell {

    lazy var coverIV: UIImageView = {
       let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = false
        imageView.image = UIImage(named: "icon_music_def")
        imageView.allRound(8)
        return imageView
    }()
    
    lazy var stateIV: UIImageView = {
       let imageView = UIImageView()
        imageView.image = UIImage(named: "icon_music_player")
        return imageView
    }()
    
    lazy var titleLb: UILabel = {
       let label = UILabel()
        label.text = "我的女朋友白雪(My Dragon Lady)"
        label.textColor = Theme.color.text_d1
        label.font = UIFont.pingFangSCRegular(ofSize: 14)
        return label
    }()
    
    lazy var descLb: UILabel = {
       let label = UILabel()
        label.text = "2024-06-03 19:34:12"
        label.textColor = Theme.color.text_d3
        label.font = UIFont.pingFangSCRegular(ofSize: 11)
        return label
    }()
    
    lazy var moreBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.imageForNormal = UIImage(named: "icon_music_more")
        return button
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .white
        selectionStyle = .none
        contentView.addSubviews([coverIV, stateIV, titleLb, descLb, moreBtn])
        coverIV.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview().inset(5)
            make.leading.equalToSuperview().offset(15)
            make.width.height.equalTo(60)
        }
        
        stateIV.snp.makeConstraints { make in
            make.centerY.centerX.equalTo(coverIV)
        }
        
        titleLb.snp.makeConstraints { make in
            make.top.equalTo(coverIV.snp.top).offset(8)
            make.leading.equalTo(coverIV.snp.trailing).offset(8)
        }
        
        descLb.snp.makeConstraints { make in
            make.bottom.equalTo(coverIV.snp.bottom).offset(-8)
            make.leading.equalTo(coverIV.snp.trailing).offset(8)
        }
        
        moreBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-15)
            make.width.height.equalTo(30)
        }
    }
    
    
    func setup(model:MusicModel){

        if model.sts == 1 {
            self.coverIV.image = UIImage(named: "icon_music_def")
            self.stateIV.image = UIImage(named: "icon_music_generate")
            self.titleLb.text = "生成中..."
            self.titleLb.textColor =  Theme.color.text_d3
        }else if model.sts == 2 {
            self.coverIV.sd_setImage(with: URL(string: model.cover_url))
            self.stateIV.image = UIImage(named: "icon_music_player")
            self.titleLb.text = model.music_name ?? ""
            self.titleLb.textColor =  Theme.color.text_d1
        }else if model.sts == 3 {
            self.coverIV.image = UIImage(named: "icon_music_def")
            self.titleLb.text = "生成失败"
            self.titleLb.textColor = UIColor(hexString: "#FF2727")
            self.stateIV.image = UIImage(named: "icon_music_error")
        }
        self.descLb.text = model.create_time
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
